<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		
		<!-- 搜索结果列表 -->
		<view class="sugg-list" v-if="searchResults.length != 0">
		  <view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoDetail(item.goods_id)">
		    <view class="goods-name">{{item.goods_name}}</view>
		    <uni-icons type="arrowright" size="16"></uni-icons>
		  </view>
		</view>
		
		<!-- 搜索历史 -->
		<view class="sugg-history" v-else>
		  <!-- 标题区域 -->
		  <view class="history-title">
		    <text>搜索历史</text>
		    <uni-icons type="trash" size="17"  @click="clean"></uni-icons>
		  </view>
		  <view class="search-history-list">
		    <uni-tag  :text="item" v-for="(item, i) in history" :key="i" >{{item[i]}}</uni-tag>
		  </view>
		</view>
	</view>
	
</template>

<script>
	export default {
		data() {
			return {
				// 延时器的 timerId
				timer: null,
				// 搜索关键词
				kw: '',
				// 搜索历史列表
				queryHistroyList:['s','d','k'],
				// 搜索结果列表
				searchResults: []
			}
		},
		methods: {
			async input(e) {
				// 清除延时器
				clearTimeout(this.timer)
				// 输入框输入后每500毫秒搜索一次
				setTimeout(()=>{
					 // e.value 是最新的搜索内容
					// console.log(e)
					this.kw = e.value
					this.seachList()
				},500)
			    
			},
			async seachList(){
				console.log(this.kw)
				if (this.kw.length===0) {
					this.searchResults = []
					return
				}
				const {data:res} =await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
				console.log(res)
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message
				this.saveSearchHistory() 
			},
			gotoDetail(goods_id){
				uni.navigateTo({
					url:"/subpkg/goods_detail/goods_detail?goods_id="+goods_id
				})
			},
			saveSearchHistory(){
				
				// 1. 将 Array 数组转化为 Set 对象
				const queryHistory = new Set(this.queryHistroyList)
				// 2. 调用 Set 对象的 delete 方法，移除对应的元素
				queryHistory.delete(this.kw)
				 // 3. 调用 Set 对象的 add 方法，向 Set 中添加元素
				queryHistory.add(this.kw)
				// 4. 将 Set 对象转化为 Array 数组
				this.queryHistroyList = Array.from(queryHistory)
				// 将搜索历史存储到本地
				uni.setStorageSync('kw',JSON.stringify(this.queryHistroyList))
			},
			clean(){
				this.queryHistroyList = []
				uni.setStorageSync('kw','[]')
			}
		},
		computed:{
			history(){
				return [...this.queryHistroyList].reverse()
			}
		},
		onLoad() {
			this.queryHistroyList = JSON.parse(uni.getStorageSync("kw" ) || [])
		}
	}
</script>

<style lang="scss" scoped>
	.search-box {
	    position: sticky;
	    top: 0;
	    z-index: 999;
	}
	
	.sugg-list{
		padding: 0 5px;
	}
	.sugg-item{
		display: flex;
		font-size: 12px;
		overflow: hidden;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		align-items: center;
		justify-content: space-between;
		
		.goods-name {
		    // 文字不允许换行（单行文本）
		    white-space:nowrap;
		    // 溢出部分隐藏
		    overflow: hidden;
		    // 文本溢出后，使用 ... 代替
		    text-overflow: ellipsis;
		    margin-right: 3px;
		}
	}
	.history-title{
		display: flex;
		justify-content: space-between;
		padding: 8px;
	}
	.search-history-list{
		display: flex;
		// justify-content: space-around;
		flex-wrap: wrap;
		.uni-tag {
		      margin-top: 5px;
		      margin-right: 5px;
		}
		
	}

</style>
